/*
* @Author: Marte
* @Date:   2017-07-29 09:29:41
* @Last Modified by:   Marte
* @Last Modified time: 2017-09-17 16:55:01
*/

'use strict';
function getStyle(obj,arrt){

    return obj.currentStyle?obj.currentStyle[arrt]:getComputedStyle(obj)[arrt];

}

function $(v){
    if(typeof v=='function'){
        window.onload=v;
    }else if(typeof v=='string'){
        return document.getElementById(v);
    }else if(typeof v=='object'){
        return v;
    }
}
//时间转换；
function addZero(t){
    return t > 10 ? t : "0" + t;
}


// JavaScript Document

function $( v ){
    if( typeof v === 'function' ){
        window.onload = v;
    } else if ( typeof v === 'string' ) {
        return document.getElementById(v);
    } else if ( typeof v === 'object' ) {
        return v;
    }
}




function doMove(obj,speed,end,attr,endFn){
    clearInterval(obj.timer);

    speed = parseInt(getStyle(obj,attr))<end ? speed : -speed;

    obj.timer = setInterval(function (){
        var next = parseInt(getStyle(obj,attr))+speed;

        if(next>=end&&speed>0||next<=end&&speed<0){
            next = end;
        }

        obj.style[attr] = next+"px";

        if(parseInt(getStyle(obj,attr))==end){
            clearInterval(obj.timer);
            if(endFn){endFn();}
            //endFn&&endFn();
        }
    },30)
}
//时间对象
function nowTime(){
    var myTime = new Date();
    var iYear=myTime.getFullYear();
    var iMonth=myTime.getMonth(); //从0开始，因此要+1
    var iDate=myTime.getDate();
    var iDay=myTime.getDay();
    var iHours=myTime.getHours();
    var iMinutes=myTime.getMinutes();
    var iSeconds=myTime.getSeconds();

    switch(iDay){
        case 0:iDay="星期日";break;
        case 1:iDay="星期一";break;
        case 2:iDay="星期二";break;
        case 3:iDay="星期三";break;
        case 4:iDay="星期四";break;
        case 5:iDay="星期五";break;
        case 6:iDay="星期六";break;

    }

//$("p1").innerHTML=iYear+"年"+iMonth+"月"+iDate+"日"+iDay+" "+addZero(iHours)+":"+addZero(iMinutes)+":"+addZero(iSeconds);
}
//添加一个class
function addClass(obj,sClass){
    var arr=obj.className.split(" ");
    if(arrIndexOf(arr,sClass)==-1){
        obj.className+=" "+sClass;
    }

}
//返回sclass下标
function arrIndexOf(arr,sClass){
    for(var i=0;i<arr.length;i++){
        if(arr[i]==sClass){
            return i;
        }
    }
    return -1;
}
//删除一个class
function removeClass(obj,sClass){
    var arr=obj.className.split(" ");
    if(arrIndexOf(arr,sClass)!=-1){
        arr.splice(arrIndexOf(arr,sClass),1);
        obj.className=arr.join(" ");
    }
}

//倒计时
//var endTime=new Date(2017,8,6,17,16,0,0);这里要写截止时间
function daojishi(){
    var nowTime=new Date();
    var t=Math.floor((endTime-nowTime)/1000);
    var str = addZero(Math.floor(t/86400))+'天'+addZero(Math.floor(t%86400/3600))+'时'+addZero(Math.floor(t%3600/60))+'分'+addZero(t%60)+'秒';
    $("p1").innerHTML=str;
}


//事件调用，冒泡，捕获，obj元素  evname  :事件    最后一个参数为true时会发现弹出顺序和之前相反了
function bind(obj,evname,fn){
    if(obj.addEventListener){
        obj.addEventListener(evname,fn,false)
    }else{obj.attachEvent('on'+evname,function(){
        fn.call(obj);})
    }}

//cookie  封装  cookie
function setCookie(key,value,t){
    var oDate = new Date();
    oDate.setDate(oDate.getDate()+t);
    document.cookie=key+'='+value+';expires='+oDate.toGMTString();
}
//setCookie("catName","Tom",30);// 保存三十天

//得到cookie
function getCookie(key){
    var arr1 = document.cookie.split('; ');
    for(var i=0;i<arr1.length;i++){
        var arr2 = arr1[i].split('=');
        if(arr2[0]==key){return decodeURI(arr2[1]);}
    }
}

//删除cookie
function removeCookie(key){
    setCookie(key,'',-1);
}

//自己的轮播图封装
//arr 是放图片的数组，obj 是要把图面放到img的src中！
function lunBo(arr,obj){
    var i=0;
    setInterval(function(){
        if(i==arr.length){
            i=0;
        }
        obj.getElementsByTagName("img")[0].src=arr[i];
        i++;
    },1000)
}

